import HttpUtil from '@/common/HttpUtil'

const ResourceApi = {
  findResourcePage (params) {
    let postUrl = process.env.SURVEY_DOMAIN + '/gr/resource/findResourcePage'
    return HttpUtil.postJSON(postUrl, params)
  },
  saveResource (params) {
    let url = process.env.SURVEY_DOMAIN + '/gr/resource/saveResource'
    return HttpUtil.postJSON(url, params)
  },
  deleteResource (param) {
    let url = process.env.SURVEY_DOMAIN + '/gr/resource/deleteResource/' + param
    return HttpUtil.delete(url, null)
  },
  deleteResourceBatch(params) {
    let url = process.env.SURVEY_DOMAIN + '/gr/resource/deleteBatch'
    return HttpUtil.postJSON(url, params);
  },
  getResourceInfo (param) {
    let url = process.env.SURVEY_DOMAIN + '/gr/resource/getResourceInfo/' + param
    return HttpUtil.get(url, null)
  },
  uploadFile (params) {
    let url = process.env.SURVEY_DOMAIN + '/accessory/uploadFile'
    return HttpUtil.postFile(url, params)
  },
  /**
   * 分页获取物种管理列表
   */
  findSpeciesPage (params) {
    let postUrl = process.env.SURVEY_DOMAIN + '/gr/speciesManage/findPage'
    return HttpUtil.postJSON(postUrl, params)
  },
  /**
   * 保存物种
   * @param params
   * @returns {*}
   */
  saveSpecies (params) {
    let postUrl = process.env.SURVEY_DOMAIN + '/gr/speciesManage/save'
    return HttpUtil.postJSON(postUrl, params)
  },
  /**
   * 获取单个物种
   * @param param
   * @returns {*}
   */
  getSpecies (param) {
    let postUrl = process.env.SURVEY_DOMAIN + '/gr/speciesManage/get/' + param
    return HttpUtil.get(postUrl, null)
  },
  /**
   * 删除物种
   * @param param
   * @returns {*}
   */
  deleteSpecies (param) {
    let postUrl = process.env.SURVEY_DOMAIN + '/gr/speciesManage/delete/' + param
    return HttpUtil.delete(postUrl, null)
  },
  findSpecies () {
    let postUrl = process.env.SURVEY_DOMAIN + '/gr/speciesManage/findList'
    return HttpUtil.post(postUrl, null)
  },
  saveEvaluate (params) {
    let url = process.env.SURVEY_DOMAIN + '/gr/resourceEvaluate/save'
    return HttpUtil.postJSON(url, params)
  },
  getEvaluateInfo (param) {
    let url = process.env.SURVEY_DOMAIN + '/gr/resourceEvaluate/getInfo/' + param
    return HttpUtil.get(url, null)
  },
  deleteEvaluate (param) {
    let url = process.env.SURVEY_DOMAIN + '/gr/resourceEvaluate/delete/' + param
    return HttpUtil.delete(url, null)
  },
  findEvaluatePage (params) {
    let url = process.env.SURVEY_DOMAIN + '/gr/resourceEvaluate/findPage'
    return HttpUtil.postJSON(url, params)
  },
  findRegisterPreservation (params) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/save/findAll'
    return HttpUtil.postJSON(url, params)
  },
  findRegisterPreservationPage (params) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/save/findPage'
    return HttpUtil.postJSON(url, params)
  },
  getRegisterPreservationInfo (param) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/save/getById/' + param
    return HttpUtil.get(url, null)
  },
  deleteRegisterPreservation (param) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/save/delData/' + param
    return HttpUtil.delete(url, null)
  },
  deleteRegisterSaveBatch(params) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/save/deleteBatch'
    return HttpUtil.postJSON(url, params);
  },
  saveRegisterPreservation (params) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/save/saveData'
    return HttpUtil.postFile(url, params)
  },
  findRegisterGatherPage (params) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/gather/findRegisterGatherPage'
    return HttpUtil.postJSON(url, params)
  },
  // 林木种质采集登记
  deleteRegisterGather (param) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/gather/deleteRegisterGather/' + param
    return HttpUtil.delete(url, null)
  },
  delGatherPic (param) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/gather/delGatherPic/' + param
    return HttpUtil.delete(url, null)
  },
  findAllRegisterGatherInfo (params) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/gather/findAllRegisterGatherInfo'
    return HttpUtil.postJSON(url, params)
  },
  getRegisterGatherInfo (param) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/gather/getRegisterGatherInfo/' + param
    return HttpUtil.get(url, null)
  },
  saveRegisterGather (params) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/gather/saveRegisterGather'
    return HttpUtil.postFile(url, params)
  },
  downloadExcel (url, data) {
    url = process.env.SURVEY_DOMAIN +url
    return HttpUtil.download(url, data).then(res => {
      let headers = res.headers
      let contentType = headers['content-type']
      if (!res.data) {
        console.error('响应异常：', res)
        return false
      } else {
        const blob = new Blob([res.data], {type: contentType})
        const contentDisposition = res.headers['content-disposition']
        let fileName = 'unknown'
        if (contentDisposition) {
          fileName = window.decodeURI(res.headers['content-disposition'].split('=')[1])
        }
        if ('download' in document.createElement('a')) {
          let link = document.createElement('a')
          link.href = window.URL.createObjectURL(blob) // 创建下载的链接
          link.download = fileName // 下载后文件名
          link.style.display = 'none'
          document.body.appendChild(link)
          link.click() // 点击下载
          window.URL.revokeObjectURL(link.href) // 释放掉blob对象
          document.body.removeChild(link) // 下载完成移除元素
        } else {
        // IE10+下载
          window.navigator.msSaveBlob(blob, fileName)
        }
      }
      return true
    }).catch(err => {
      console.log(err)
      return false
    })
  },
  saveMigration(params) {
    let url = process.env.SURVEY_DOMAIN + '/gr/register/save/saveMigration'
    return HttpUtil.postJSON(url, params)
  }
}

export default ResourceApi
